/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package perso;

import carte.Carte;
import ctrl.WorldCtrl;
import ctrl.command.CmdKill;
import ctrl.command.CmdMove;
import ctrl.command.Command;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Benoît
 */
public class ImperialSoldier extends PersonnageStarWars {

    public ImperialSoldier(int id, Carte carte) {
        super(id, carte);
        setImage("/images/ImperialSoldier.png");
        name = "Imperial soldier";
    }

    @Override
    public Command doAction() {
        // CarteTruand map = (CarteTruand)carte;
        Command cmd = new CmdMove(this, px, py);
        int lower = 1;
        int higher = 5;
        int random = (int) (Math.random() * (higher - lower)) + lower;
        int pxDest = px;
        int pyDest = py;
        if (random == 1) {
            if (px + 1 < carte.getPerso().length) {
                pxDest++;
            }
        } else if (random == 2) {
            if (px - 1 > 0) {
                pxDest--;
            }
        } else if (random == 3) {
            if (py - 1 > 0) {
                pyDest--;
            }
        } else if (random == 4) {
            if (py + 1 < carte.getPerso()[0].length) {
                pyDest++;

            }
        }
        Random rnd = new Random();
        Personnage cible = carte.getPerso()[pxDest][pyDest];
        if (cible == null) {
            cmd = new CmdMove(this, pxDest, pyDest);
        } else if (cible instanceof Yoda) {
            if (rnd.nextInt(100) > 95) {
                cmd = new CmdKill(cible);
            } else {
                cmd = new CmdKill(this);
            }
        } else if (cible instanceof Jedi) {
            if (rnd.nextInt(100) > 75) {
                int esquivePx = 0;
                int esquivePy = 0;
                try {
                    Personnage esquive = carte.getPerso()[cible.px - 1][cible.py - 2];
                    if (esquive == null) {
                        esquivePx = cible.px - 1;
                        esquivePy = cible.py - 2;
                    }

                    esquive = carte.getPerso()[cible.px - 1][cible.py + 2];
                    if (esquive == null) {
                        esquivePx = cible.px - 1;
                        esquivePy = cible.py + 2;
                    }

                    esquive = carte.getPerso()[cible.px + 1][cible.py - 2];
                    if (esquive == null) {
                        esquivePx = cible.px + 1;
                        esquivePy = cible.py - 2;
                    }

                    esquive = carte.getPerso()[cible.px + 1][cible.py + 2];
                    if (esquive == null) {
                        esquivePx = cible.px + 1;
                        esquivePy = cible.py + 2;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    // esquive out of map
                    esquivePx = esquivePy = 0;
                }

                if (esquivePx != 0 && esquivePy != 0) {
                    cmd = new CmdMove(cible, esquivePx, esquivePy);
                } else {
                    cmd = new CmdKill(cible);
                }
            } else {
                cmd = new CmdKill(this);
            }
        } else if (cible instanceof RebelPilot) {
            if (rnd.nextInt(100) > 50) {
                cmd = new CmdKill(cible);
            } else {
                cmd = new CmdKill(this);
            }
        }

        return cmd;
    }

    @Override
    public void convert(ArrayList<Personnage> listPerso) {
        Sith sith = new Sith(this);
        Personnage[][] persos = carte.getPerso();
        persos[this.px][this.py] = sith;
        sith.px = this.px;
        sith.py = this.py;
        carte.setPerso(persos);
        int i = listPerso.indexOf(this);
        listPerso.set(i, sith);
        
        System.out.println("ImperialSoldier " + this.id + " converted to Sith");
    }
}
